package com.dianrui.fmi.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import java.util.Locale;

public class LocaleUtils {
    private static final String FILE_NAME = "better_language_info";
    private static final String KEY_LANGUAGE = "key_language";
    private static final String KEY_COUNTRY = "key_country";

    /**
     * 获取手机系统的Locale
     *
     * @return
     */
    public static Locale getSystemLocale() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return LocaleList.getDefault().get(0);
        }
        return Locale.getDefault();
    }

    /**
     * 设置 App 的Locale
     *
     * @param context
     * @param loc
     * @return
     */
    public static void setAppLocale(Context context, Locale loc) {
        if (loc != null) {
            setUserSetLocale(context, loc);

            Resources rs = context.getResources();
            Configuration config = rs.getConfiguration();
            DisplayMetrics dm = rs.getDisplayMetrics();
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                config.locale = loc;
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                config.setLocales(new LocaleList(loc));
            }
            context.getResources().updateConfiguration(config, dm);
        }
    }

    public static void setAppLocales(Context context, Locale loc) {
        if (loc != null) {
            setUserSetLocale(context, loc);

            Resources rs = context.getResources();
            Configuration config = rs.getConfiguration();
            DisplayMetrics dm = rs.getDisplayMetrics();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                config.setLocales(new LocaleList(loc));
            } else {
                config.locale = loc;
            }
            context.getResources().updateConfiguration(config, dm);
        }
    }



    /**
     * 在activity 的  onResume or onConfigChange中调用此方法
     * http://www.jianshu.com/p/47bbd34d2af8
     *
     * @param context
     * @param loc
     */
    public static void setLocaleWhenConfigChange(Context context, Locale loc) {
        if (loc != null) {
            Resources rs = context.getResources();
            Configuration config = rs.getConfiguration();
            DisplayMetrics dm = rs.getDisplayMetrics();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                config.setLocales(new LocaleList(loc));
            } else {
                config.locale = loc;
            }
            context.getResources().updateConfiguration(config, dm);

        }
    }

    /**
     * 获取App Locale
     *
     * @param context
     * @return
     */
    public static Locale getAppLocale(Context context) {
        Configuration config = context.getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            return config.getLocales().get(0);
        else
            return config.locale;
    }

    /**
     * 获取用户设置的Locale
     *
     * @param context
     * @return
     */
    public static Locale getUserSetLocale(Context context) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        String lang = sp.getString(KEY_LANGUAGE, "");
        String country = sp.getString(KEY_COUNTRY, "");
        if (!TextUtils.isEmpty(lang) && !TextUtils.isEmpty(country)) {
            return new Locale(lang, country);
        }
        return null;
    }

    /**
     * 返回 en_US 格式
     *
     * @param context
     * @return
     */
    public static String getUserSetLocaleStr(Context context) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        String lang = sp.getString(KEY_LANGUAGE, "");
        String country = sp.getString(KEY_COUNTRY, "");
        if (!TextUtils.isEmpty(lang) && !TextUtils.isEmpty(country)) {
            return String.format("%s_%s", lang, country);
        }
        return "";
    }

    /**
     * 清空设置，跟随系统
     *
     * @param context
     */
    public static void removeLocaleSet(Context context) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        final SharedPreferences.Editor edit = sp.edit();
        edit.putString(KEY_LANGUAGE, "");
        edit.putString(KEY_COUNTRY, "");
        edit.apply();
    }

    /**
     * 用户设置的Locale
     */
    private static void setUserSetLocale(Context context, Locale locale) {
        if (locale != null) {
            try {
                SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
                final SharedPreferences.Editor edit = sp.edit();
                edit.putString(KEY_LANGUAGE, locale.getLanguage());
                edit.putString(KEY_COUNTRY, locale.getCountry());
                edit.apply();
            } catch (Exception e) {
            }
        }
    }
}
